//
//  AlarmTool.m
//  Voice2AlarmNote
//
//  Created by onemade on 14-10-28.
//  Copyright (c) 2014年 CNPC. All rights reserved.
//

#import "AlarmTool.h"

@implementation AlarmTool
single_for_implementation(AlarmTool);

-(AppDelegate *) appDelegate
{
    return [[UIApplication sharedApplication] delegate];
}

-(BOOL)isAlarmExist:(int)identifier
{
    NSError *error = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identifier = %@", [NSNumber numberWithInt:identifier]];
    NSFetchRequest *request = [self requestWithPredicate:predicate];
    [request setFetchLimit:1];
    
    NSUInteger existCount = [[[self appDelegate] managedObjectContext] countForFetchRequest:request error:&error];
    if (existCount > 0) {
        return YES;
    }
    
    return NO;
}

-(BOOL)isExistSameTimeAlarm:(NSString *)time
{
    NSError *error = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"alarmTime = %@", time];
    NSFetchRequest *request = [self requestWithPredicate:predicate];
    [request setFetchLimit:1];
    
    NSUInteger existCount = [[[self appDelegate] managedObjectContext] countForFetchRequest:request error:&error];
    if (existCount > 0) {
        return YES;
    }
    
    return NO;
}

-(BOOL)addAlarm:(Alarm *)alarm
{
    NSError *error = nil;
    BOOL isSaveSuccess = [[[self appDelegate] managedObjectContext] save:&error];
    if (!isSaveSuccess) {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加失败" message:[NSString stringWithFormat:@"%@",[error localizedDescription]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    return isSaveSuccess;
}

-(BOOL)updateAlarm:(Alarm *)updateAlarm
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identifier = %@",updateAlarm.identifier];
    NSFetchRequest *request = [self requestWithPredicate:predicate];
    [request setFetchLimit:1];
    
    NSError *error = nil;
    NSMutableArray *mutableFetchResult = [[[[self appDelegate] managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResult == nil) {
        NSLog(@"error: %@,%@", error, [error userInfo]);
        return NO;
    }
    for (Alarm *alarm in mutableFetchResult) {
        alarm.identifier = updateAlarm.identifier;
        
        [[[self appDelegate] managedObjectContext] save:&error];
        return YES;
    }
    return NO;
}

-(BOOL)deleteAlarm:(Alarm *)deleteAlarm
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identifier = %@",deleteAlarm.identifier];
    NSFetchRequest *request = [self requestWithPredicate:predicate];
    [request setPredicate:predicate];
    [request setFetchLimit:1];
    
    NSError *error = nil;
    NSMutableArray *mutableFetchResult = [[[[self appDelegate] managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResult == nil) {
        NSLog(@"error: %@,%@", error, [error userInfo]);
        return NO;
    }
    
    for (Alarm *alarm in mutableFetchResult) {
        [[[self appDelegate] managedObjectContext] deleteObject:alarm];
        [[[self appDelegate] managedObjectContext]save:&error];
        return YES;
    }
    
    return NO;
}

-(NSMutableArray *)fetchAllAlarms
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alarm" inManagedObjectContext:[self appDelegate].managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    
    NSError *error = nil;
    return [[[[self appDelegate] managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
}

-(Alarm *)findAlarmById:(int)identifier
{
    NSMutableArray *allAlarms = [self fetchAllAlarms];
    for (Alarm *alarm in allAlarms) {
        if ([alarm.identifier intValue] == identifier) {
            return  alarm;
        }
    }
    return nil;
}

-(int)recieveLastObjectID
{
    NSMutableArray *allAlarms = [self fetchAllAlarms];
    Alarm *lastAlarm = [allAlarms lastObject];
    return [lastAlarm.identifier intValue];
}

#pragma mark - 返回一个查询请求
-(NSFetchRequest *)requestWithPredicate:(NSPredicate *)predicate
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"Alarm" inManagedObjectContext:[self appDelegate].managedObjectContext];
    [request setEntity:entity];
    [request setPredicate:predicate];
    return request;
}

@end
